package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.*;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.OutComeRepository;
import com.yegs.jltqc.dao.QcProduceRepository;
import com.yegs.jltqc.dao.WorkFlowRepository;
import com.yegs.jltqc.service.OutComeService;
import com.yegs.jltqc.service.QcProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class QcProduceServiceImpl implements QcProduceService {

    @Autowired
    private QcProduceRepository qcProduceRepository;

    @Autowired
    private WorkFlowRepository workFlowRepository;

    @Override
    public QcProduce save(QcProduce qcProduce) {
        qcProduce.setCreateTime(new Date());
        qcProduce.setCreateBy(ConcurentCache.getUserName());
        qcProduce = qcProduceRepository.save(qcProduce);
        WorkFlow workFlow = new WorkFlow();
        workFlow.setUserType(QcProduceWorkFlow.getNext());
        workFlow.setRefId(qcProduce.getId());
        workFlow.setCreateBy(ConcurentCache.getUserName());
        workFlow.setCreateTime(new Date());
        workFlowRepository.save(workFlow);
        return qcProduce;
    }

    @Override
    public void generateWorkflow(Long id) {
        QcProduce qcProduce = qcProduceRepository.getOne(id);
        if(qcProduce == null){
            throw new StoreException("错误单据");
        }
        WorkFlow workFlow = new WorkFlow();
        workFlow.setRefId(qcProduce.getId());
        workFlow.setCreateBy(ConcurentCache.getUserName());
        workFlow.setCreateTime(new Date());
        workFlowRepository.save(workFlow);
    }

    @Override
    public List<QcProduce> list() {
        return qcProduceRepository.findAll();
    }

    @Override
    public QcProduce get(Long id) {
        return qcProduceRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        qcProduceRepository.deleteById(id);
    }

    @Override
    public Page<QcProduce> query(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return qcProduceRepository.findAll(request);
        }
        return qcProduceRepository.findQcProduceByName(query.getName(), request);
    }

    @Override
    public Long getNext(Long id) {
        if(id == null){
            QcProduce qcProduce = qcProduceRepository.findFirstByOrderByIdDesc();
            if(qcProduce != null) {
                return qcProduce.getId();
            }
        }
        QcProduce qcProduce = qcProduceRepository.findFirstByIdIsAfter(id);
        if(qcProduce == null){ qcProduce = qcProduceRepository.findFirstByOrderByIdDesc(); }
        if(qcProduce != null){
            return qcProduce.getId();
        }
        return null;
    }

    @Override
    public Long getLast(Long id) {
        if(id == null){
            QcProduce qcProduce = qcProduceRepository.findFirstByOrderByIdDesc();
            if(qcProduce != null) {
                return qcProduce.getId();
            }
        }
        else{
            QcProduce qcProduce = qcProduceRepository.findFirstByIdBeforeOrderByIdDesc(id);
            if(qcProduce == null){ qcProduce = qcProduceRepository.findFirstByOrderById(); }
            if(qcProduce != null){
                return qcProduce.getId();
            }
        }
        return null;
    }
}
